<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
		<link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" media="all">
		<link rel="stylesheet" href="../../layuiadmin/style/admin.css" media="all">
		<link rel="stylesheet" href="../../css/common.css" media="all">
	</head>
	<body>
		<div class="layui-fluid">
			<div class="layui-row layui-col-space15">
				<div class="layui-col-md12">
					<div class="layui-btn-sm2 layui-col-md2 layui-col-xs2">
						<div id="test" class="demo-tree demo-tree-box" style="width: 100%;"></div>
					</div>
				</div>
			</div>
		</div>

		<script src="../../layuiadmin/layui/layui.js"></script>
		<script src="../../js/jquery.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../js/common.js"></script>
		<script>
			var comeData = {
				id: getParam('id'),
			}
			layui.config({
				base: '../../layuiadmin/' //静态资源所在路径
			}).extend({
				index: 'lib/index' //主入口模块
			}).use(['index', 'tree', 'table', 'form'], function() {
				var table = layui.table,
					$ = layui.$,
					tree = layui.tree,
					form = layui.form,
					admin = layui.admin;
				var _data = {
					page: 1,
					limit: 10,
					filterRules: [],
					sort: [],
					fid: '', 
				}
				initdata();
				function initdata() {
					$.post(server.shop_query, {
						member_id:cookie.GetCookie("shop_memberid")
					}, function(res) {
						if(res.code == 0) {
							var data = res.shop.data;
							const treeData = fnSetTreeData1(data);
							tree.render({
								elem: '#test',
								data: treeData,
								onlyIconControl: true,
								accordion: true ,
								click: function(obj) {
									var data = obj.data;
									parent.$('input[name=shopid]').val(data.sub_shopid);
									parent.$('div[name=shopname]').html(data.sub_shopname);
									localStorage.setItem('pxy_shopid', data.sub_shopid);
									localStorage.setItem('pxy_shopname', data.sub_shopname);
									parent.layer.msg("正在为您切换店铺。。。");
									parent.location.reload();
									parent.layer.closeAll();
								}
							});
						} else {
							layer.msg(res.msg);
						}
					});
				}
				function fnSetTreeData1(data) {
					var data = [...data];
					var tree = data.filter((father)=>{
						father.id = father.sub_shopid
						father.title = father.sub_shopname;
						if(comeData.id == father.sun_shopid){
							father.checked = true;
						}
						var branchArr = data.filter((child) => {
							if(child.shopid != child.sub_shopid){
								if(child.shopid == father.sub_shopid) child._hasParent = true;
								return child.shopid == father.sub_shopid
							}
						});
						if (branchArr.length > 0) father.children = branchArr;
						return !father._hasParent;
					})
					tree = tree.filter((item) => {
						item.spread = true;
						return !item._hasParent;
					})
					return tree
				}
			});
		</script>
	</body>
</html>
